<?php

namespace bc\controller;

use bc\util\Util;
use bc\ar\MActiveRecord;
use bc\wx\OAuth;

class GameController extends FrontController
{
	
	
	/**
	 * 微信授权
	 */
	public function wechatAuth()
	{
		$openid = $_COOKIE['wx_openid'];
		$code = $this->get('code', '');
		if(!$openid)
		{
			if($code)
			{
				//获取用户状态
				$appid = $this->wechat()->appid;
				$secret = $this->wechat()->secret;
				$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
				$rs = Util::httpGet($url);
				$arr = json_decode($rs, true);
				
				
				$dao = new MActiveRecord();
				$sql = "select uid, subscribe, wxid from wxch_user where wxid = '{$arr['openid']}'";
				$row = $dao->getDb()->createCommand($sql)->queryOne();
				
				setcookie('wx_openid', $arr['openid'], time()+3600*24*30);
				
				$_SESSION['wx_openinfo'] = $row;
				
			}
			else 
			{
				//跳转授权拿用户信息
				$auth = new OAuth();
				$redirectUri = $this->getFullUrl();
				$auth->authorize($redirectUri, OAuth::SNSAPI_BASE);
			}
		}
	}
	

	
}

